<?php

namespace yiike\order\backend\controllers;
use yii\helpers\Url;
use dmstr\bootstrap\Tabs;
use yiike\order\model\OrderStore;
use yiike\order\model\OrderGoods;
use yiike\order\model\Express;
use yiike\order\model\OrderStoreSearch;
/**
* This is the class for controller "OrderStoreController".
*/
class OrderStoreController extends \yiike\order\backend\controllers\base\OrderStoreController
{
    public function actionStoreOrder($store_id)
    {

        $_GET['OrderStoreSearch']['store_id'] = $store_id;
        $searchModel  = new OrderStoreSearch;
        $dataProvider = $searchModel->search($_GET);

        Tabs::clearLocalStorage();

        Url::remember();
        \Yii::$app->session['__crudReturnUrl'] = null;

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }
    public function actionView($id)
    {

        $model = $this->findModel($id);

        $area = $model->order->province.'-'.$model->order->city.'-'.$model->order->district;
        $express = Express::find()->select('code,name')->where(['status' => 'y'])->asArray()->all();

        foreach($express as $k=>$v){
            $arr[$v['code']] = $v['name'];
        }
        $express = $arr;
        if(empty($model->order->province)){
            $area='';
        }
        $model->area = $area;
        $order_id = $model->order_id;

        $goods = OrderGoods::find()->where(['id'=>$order_id])->asArray()->all();
        \Yii::$app->session['__crudReturnUrl'] = Url::previous();
        Url::remember();
        Tabs::rememberActiveState();

        return $this->render('view', [
            'model' => $model,
            'goods'=>$goods,
            'express'=>$express
        ]);
    }
    //订单发货
    public function actionSend($id)
    {
        $model = OrderStore::findOne($id);

        $model->load($_POST);
        $express_name = Express::find()->select('name')->where(['code'=>$model->express_code])->asArray()->one();
        $express_name = $express_name['name'];
        $model->express_name = $express_name;
        $model->status = 2;
        $model->express_at = date('Y-m-d H:i:s',time());
        if ($model->save()) {
            return $this->redirect(Url::previous());
        } else {
            return $this->redirect(Url::toRoute(['view','id'=>$model->id]));
        }
    }

}
